const path = require('path')
const {app, Menu, Tray} = require('electron')

import {createFromPath} from "../utils/NativeImageUtils"
import {createNotification} from "../js/notification"

let appIcon = null

export const createTray = () => {
    const iconPath = path.join(__dirname, 'static/img/n-128.png')
    appIcon = new Tray(createFromPath(iconPath, 18, 18))
    const contextMenu = Menu.buildFromTemplate([
        {
            label: 'Item2',
            submenu: [
                {label: 'submenu1'},
                {label: 'submenu2'}
            ]
        },
        {
            label: 'Item3',
            type: 'checkbox',
            checked: true
        },
        {
            label: 'Item3',
            type: 'radio',
            checked: true
        },
        {
            type: 'separator'
        },
        {
            label: 'Set up',
            icon: createFromPath(iconPath, 18, 18),
            click: () => {
                let notification = createNotification("My Title", "My Body");
                notification.show()
                notification.on('click', (event)=>{
                    console.log(notification.title)
                })
            }
        },
        {
            type: 'separator'
        },
        {
            label: 'Logout',
            click: () => {
                app.quit();
            }
        },
        {
            label: 'Quit',
            click: () => {
                app.quit();
            }
        }
    ])
    appIcon.setToolTip('struggle-electron')
    appIcon.setContextMenu(contextMenu)
}

app.on('window-all-closed', () => {
    if (appIcon) {
        appIcon.destroy()
    }
})
